/**
 * @file gui.c
 * @author Yao Zongyou <yaozy2010@gmail.com>
 * @date 2011-04-05
 */

#include "master.h"

#include <shlwapi.h>
#include <commctrl.h>

#include "gui.h"
#include "comfunct/comfunct.h"

void ControlCreate(HWND hwnd)
{
     HWND hVolumeSlider = NULL, hVolumeText = NULL, hFileName = NULL, hSlider = NULL, hAudioText = NULL,
		  hPlay = NULL, hPause = NULL, hStop = NULL, hOpenFile = NULL, hStatus = NULL, hElapsedTime = NULL,
		  hTotalTime = NULL, hDivide = NULL, hLyricsBox = NULL, hLyricsTitle = NULL, hPlaylistTitle = NULL,
		  hPlaylistItems = NULL;
     const int iStatwidths[] = {70, 280, -1};
     int iTrackbarWidth = 0;
     extern LPTSTR szVolume;
     extern signed long PlayVolume;
	 LVCOLUMN lvc = {0};
	 HGDIOBJ hfDefault = NULL;
	 HMODULE hThemeDll = LoadLibrary(TEXT("uxtheme.dll")), hAudioPlayer = GetModuleHandle(NULL);
	 
	 if(hThemeDll == NULL)
	 {
          iTrackbarWidth = 17; /* Make the width of the slider tabs smaller on 9x/Me */
     }
     else
     {
          iTrackbarWidth = 21;
          FreeLibrary(hThemeDll);
     }
     
	 WindowCreate(&hAudioText, TEXT("EDIT"), GetLocalText(0), WS_CHILD | WS_DISABLED | ES_MULTILINE | ES_READONLY | SS_CENTER | WS_VISIBLE, 6, 13, 33, 28, hwnd, IDI_AUDIOTEXT, hAudioPlayer);
     WindowCreate(&hFileName, TEXT("EDIT"), TEXT(""), WS_CHILD | ES_AUTOHSCROLL | ES_LEFT | WS_VISIBLE | SS_NOPREFIX, 42, 19, 208, 13, hwnd, IDI_FILENAME, hAudioPlayer);
     WindowCreate(&hOpenFile, TEXT("BUTTON"), TEXT("..."), WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | BS_TEXT, 254, 18, 20, 15, hwnd, IDI_FILE_OPEN, hAudioPlayer);
     WindowCreate(&hPlay, TEXT("BUTTON"), GetLocalText(1), WS_CHILD | WS_VISIBLE | WS_DISABLED | BS_PUSHBUTTON | BS_TEXT, 14, 43, 50, 19, hwnd, IDI_CONT_PLAY, hAudioPlayer);
     WindowCreate(&hPause, TEXT("BUTTON"), GetLocalText(2), WS_CHILD | WS_VISIBLE | WS_DISABLED | BS_PUSHBUTTON | BS_TEXT, 110, 43, 56, 19, hwnd, IDI_CONT_PAUSE, hAudioPlayer);
     WindowCreate(&hStop, TEXT("BUTTON"), GetLocalText(3), WS_CHILD | WS_VISIBLE | WS_DISABLED |  BS_PUSHBUTTON | BS_TEXT, 231, 43, 48, 19, hwnd, IDI_CONT_STOP, hAudioPlayer);
     WindowCreate(&hVolumeText, TEXT("EDIT"), GetLocalText(4), WS_CHILD | WS_VISIBLE | WS_DISABLED | SS_CENTER, 280, 2, 35, 17, hwnd, IDI_VOLTEXT, hAudioPlayer);
     WindowCreate(&hSlider, TEXT("msctls_trackbar32"), TEXT(""), WS_CHILD | WS_VISIBLE | WS_DISABLED | WS_GROUP | TBS_HORZ | TBS_NOTICKS, 19, 65, 245, iTrackbarWidth, hwnd, IDI_SLIDER, hAudioPlayer);
     WindowCreate(&hVolumeSlider, TEXT("msctls_trackbar32"), TEXT(""), WS_CHILD | WS_VISIBLE | ES_READONLY | WS_DISABLED | TBS_NOTICKS | TBS_VERT, 290, 13, iTrackbarWidth, 85, hwnd, IDI_VOLUME, hAudioPlayer);
     WindowCreate(&hElapsedTime, TEXT("EDIT"), TEXT(""), WS_CHILD | WS_VISIBLE | WS_DISABLED | ES_READONLY | SS_CENTER, 95, 86, 41, 13, hwnd, IDI_ELAPSED_TIME, hAudioPlayer);
     WindowCreate(&hDivide, TEXT("EDIT"), TEXT("/"), WS_CHILD | WS_DISABLED | SS_LEFT, 137, 86, 5, 13, hwnd, IDI_DIVIDE, hAudioPlayer);
     WindowCreate(&hTotalTime, TEXT("EDIT"), TEXT(""), WS_CHILD | WS_VISIBLE | WS_DISABLED | ES_READONLY | SS_CENTER, 144, 86, 45, 13, hwnd, IDI_TOTAL_TIME, hAudioPlayer);

     WindowCreate(&hLyricsTitle, TEXT("EDIT"), GetLocalText(5), WS_CHILD | WS_VISIBLE | WS_DISABLED | ES_READONLY | SS_CENTER, 0, 130, 290, 15, hwnd, IDI_LYRICS_TITLE, hAudioPlayer);
	 hLyricsBox = CreateWindowEx(WS_EX_WINDOWEDGE, TEXT("EDIT"), TEXT(""), WS_CHILD | WS_VSCROLL | WS_VISIBLE | ES_MULTILINE | ES_LEFT | ES_WANTRETURN | ES_AUTOVSCROLL | ES_READONLY,
                                3, 150, 330, 170, hwnd, (HMENU)IDI_LYRICS, hAudioPlayer, NULL);
	 WindowCreate(&hPlaylistTitle, TEXT("EDIT"), GetLocalText(6), WS_CHILD | WS_VISIBLE | WS_DISABLED | ES_READONLY | SS_CENTER, 325, 0, PLAYLIST_VIEW_WIDTH, 15, hwnd, IDI_PLAYLIST_TITLE, hAudioPlayer);
	 hPlaylistItems = CreateWindow(WC_LISTVIEW, TEXT(""), WS_CHILD | WS_VISIBLE | LVS_SINGLESEL | LVS_REPORT | LVS_NOCOLUMNHEADER, 335, 15, PLAYLIST_VIEW_WIDTH, 125, hwnd,
									(HMENU)IDI_PLAYLIST_VIEW, hAudioPlayer, NULL);
	 ListView_SetExtendedListViewStyleEx(hPlaylistItems, 0, 0x00000020); /* LVS_EX_FULLROWSELECT not in my version of the MinGW headers so I use its numeric value here */
	 lvc.mask = LVCF_FMT | LVCF_WIDTH;
	 lvc.fmt = LVCFMT_LEFT;
	 ListView_InsertColumn(hPlaylistItems, 1, &lvc);
	 lvc.cx = PLAYLIST_VIEW_WIDTH;
	 ListView_InsertColumn(hPlaylistItems, 0, &lvc);

     hStatus = CreateStatusWindow(WS_VISIBLE | WS_CHILD, GetLocalText(7), hwnd, IDI_STATUS);
     SendDlgItemMessage(hwnd, IDI_STATUS, SB_SETPARTS, 3, (LPARAM)iStatwidths);
     
//     SendDlgItemMessage(hwnd, IDI_VOLUME, TBM_SETRANGE, true, MAKELONG(0, (VOLUME_MUTE * -1)));
//     SendDlgItemMessage(hwnd, IDI_VOLUME, TBM_SETPOS, TRUE, (LONG)0);
//     szVolume = VolumeTextFromLong(PlayVolume, szVolume);
//     SetStatus(hwnd, IDI_STATUS, 2, szVolume);
     
     hfDefault = GetStockObject(DEFAULT_GUI_FONT);
     ChangeTextFont(hwnd, hfDefault);
     SetFocus(hFileName);
//     SHAutoComplete(hFileName, SHACF_AUTOSUGGEST_FORCE_ON | SHACF_URLALL | SHACF_FILESYSTEM | SHACF_USETAB);
}

void WindowCreate(HWND *name, LPCTSTR szClassname, LPCTSTR szWindowText, DWORD dwStyle, int xpos, int ypos, int width, int height, HWND hwndParent, int ControlID, HINSTANCE hInstance)
{
	*name = CreateWindow(szClassname, szWindowText, dwStyle, xpos, ypos, width, height, hwndParent, (HMENU)ControlID, hInstance, NULL);
	if (*name == NULL) {
		ErrorInfo();
		ErrorMessage(GetLocalText(8));
	}
}

void ToggleButton(HWND hwnd, bool bState, bool bPlayDisable)
{

}

//void ElapsedTimeUpdate(HWND, PEFERENCE_TIME);

void TotalTimeUpdate(HWND hwnd)
{

}

void ClearTimes(HWND hwnd)
{

}

LPTSTR VolumeTextFromLong(signed long n, LPTSTR a)
{
	return NULL;
}

TCHAR * GetStatusText(HWND hwnd, int a)
{
	return NULL;
}

void ChangeBackgroundColor(HWND hwnd, HBRUSH *hbrBackBrush)
{

}

BOOL CALLBACK SetChildWindowsTransparency(HWND hwnd, LPARAM a)
{
	return TRUE;
}

void ChangeGuiFont(HWND a, HFONT *b)
{

}

BOOL CALLBACK ChangeTextColor(HWND a, LPARAM b)
{
	return TRUE;
}

void AdjustWindowHeight(HWND a, int b)
{

}

void AdjustWindowWidth(HWND a, int b)
{

}

HWND GetAPWindow(void)
{
	HWND hwnd = {0};
	return hwnd;
}
